import { Upload } from '@/api/interface'
import { PORT1, FILE } from '@/api/config/servicePort'
import http from '@/api'

export enum UploadMode {
  URL = '1',
  OBJECT = '0'
}

/**
 * @name 文件上传模块
 */
// 图片上传
export const uploadImg = (params: FormData) => {
  return http.post<Upload.ResFile>(FILE + `/upload/img`, params)
}

// 视频上传
export const uploadVideo = (params: FormData) => {
  return http.post<Upload.ResFileUrl>(PORT1 + `/file/upload/video`, params)
}

export function upload(bucketName: string, formData: FormData, mode?: UploadMode) {
  let url = FILE + '/upload/' + bucketName
  if (mode === UploadMode.URL) {
    url += '?simple=' + mode
  }
  return http.post<Upload.ResFile>(url, formData)
}

// 上传
export async function uiUpload(bucketName: string, formData: FormData) {
  try {
    const { data } = await upload(bucketName, formData, UploadMode.URL)
    return { data: { fileUrl: data } }
  } catch (e) {
    return { fileUrl: null }
  }
}
